<?php

namespace App\Http\Controllers;

use App\Curl;
use Illuminate\Http\Request;
use DB;

class PushMessageController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }


    /**
     * 推送消息
     * @param Request $request
     * @return array|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @throws \Exception
     */
    public function push(Request $request)
    {
        if ($request->isMethod('get')) {
            return view('pushMessage.pushMessahe');
        }
        //发送消息
//        $params = ['nei' => (strip_tags($request->nei)), 'apns' => true];
        return Curl::PushMessage(strip_tags($request->nei),strip_tags($request->url)?strip_tags($request->url):0);
//        //存库
//        if (!DB::table('jpush')->insert([
//            'title' => '系统消息',
//            'content' => strip_tags($request->nei),
//            'ctime' => date('Y-m-d H:i:s', time()),
//            'url' => $request->url
//        ])) {
//            return ['status'=>2, 'message' => '发送失败'];
//        }
//        Curl::PostCurl(Curl::CURL_PUSH, $params);
//        return ['status' => 1, 'message' => '发送成功'];
    }

    /**
     * 测试推送消息
     * @param Request $request
     * @return array|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function pushcs(Request $request)
    {
        if ($request->isMethod('get')) {
            return view('pushMessage.pushMessahe');
        }
        //发送消息
        $params = ['nei' => (strip_tags($request->nei)), 'apns' => false];
        //存库
        if (!DB::table('jpush')->insert([
            'title' => '系统消息',
            'content' => strip_tags($request->nei),
            'ctime' => date('Y-m-d H:i:s', time()),
            'url' => $request->url
        ])) {
            return ['status'=>2, 'message' => '发送失败'];
        }
        Curl::PostCurl(Curl::CURL_PUSH, $params);
        return ['status' => 1, 'message' => '发送成功'];
    }


    public function sendMobileMessage()
    {
        $return = Curl::SendAllMessage();
        if ($return['errmsg'] !== 'Ok') {
            return ['status'=>2, 'message' => '发送失败'];
        }
        return ['status' => 1, 'message' => '发送成功'];
    }
}
